package cn.elwy.kit;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.CoolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import cn.elwy.kit.file.action.FileRenameAction;

public class AppMain extends ApplicationWindow {

  private static AppMain window;
  private Action action;

  /**
   * Create the application window,
   */
  public AppMain() {
    super(null);
    createActions();
    addCoolBar(SWT.FLAT);
    addMenuBar();
    addStatusLine();
  }

  /**
   * Create contents of the application window.
   * @param parent
   */
  @Override
  protected Control createContents(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);
    FillLayout fl_container = new FillLayout(SWT.HORIZONTAL);
    fl_container.marginWidth = 5;
    container.setLayout(fl_container);

    SashForm sashForm = new SashForm(container, SWT.NONE);

    CTabFolder treeTabFolder = new CTabFolder(sashForm, SWT.BORDER);
    treeTabFolder
        .setSelectionBackground(Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));

    CTabItem navItem = new CTabItem(treeTabFolder, SWT.NONE);
    navItem.setText("导航视图");

    CTabFolder mainTabFolder = new CTabFolder(sashForm, SWT.BORDER);
    mainTabFolder
        .setSelectionBackground(Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));

    CTabItem mainItem = new CTabItem(mainTabFolder, SWT.NONE);
    mainItem.setText("简介");
    sashForm.setWeights(new int[] { 200, 500 });

    return container;
  }

  /**
   * Create the actions.
   */
  private void createActions() {
    // Create the actions
    {
      action = new Action("New Action") {
        @Override
        public void run() {
        }
      };
    }
  }

  /**
   * Create the menu manager.
   * @return the menu manager
   */
  @Override
  protected MenuManager createMenuManager() {
    MenuManager menuManager = new MenuManager("menu");
    return menuManager;
  }

  /**
   * Create the coolbar manager.
   * @return the coolbar manager
   */
  @Override
  protected CoolBarManager createCoolBarManager(int style) {
    CoolBarManager coolBarManager = new CoolBarManager(style);

    ToolBarManager toolBarManager = new ToolBarManager();
    coolBarManager.add(toolBarManager);
    toolBarManager.add(new FileRenameAction());

    ToolBarManager toolBarManager_1 = new ToolBarManager();
    coolBarManager.add(toolBarManager_1);
    toolBarManager_1.add(action);
    return coolBarManager;
  }

  /**
   * Create the status line manager.
   * @return the status line manager
   */
  @Override
  protected StatusLineManager createStatusLineManager() {
    StatusLineManager statusLineManager = new StatusLineManager();
    return statusLineManager;
  }

  /**
   * Launch the application.
   * @param args
   */
  public static void main(String args[]) {
    try {
      window = new AppMain();
      window.setBlockOnOpen(true);
      window.open();
      Display.getCurrent().dispose();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Configure the shell.
   * @param newShell
   */
  @Override
  protected void configureShell(Shell newShell) {
    super.configureShell(newShell);
    newShell.setText("Elwy工具箱");
  }

  /**
   * Return the initial size of the window.
   */
  @Override
  protected Point getInitialSize() {
    return new Point(800, 600);
  }

  public static Shell getCurrentShell() {
    return window.getShell();
  }
}
